package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.model.Admin;
import com.bojiu.lawsearch.lawsearch.model.GoodsSetting;
import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Setting;
import com.bojiu.lawsearch.lawsearch.service.AdminService;
import com.bojiu.lawsearch.lawsearch.util.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author: afdiao
 * @Date：2020/3/10 21:20
 * @Description: 管理员操作类别
 */
@Api(description = "后台管理员相关接口，功能已完成！！！")
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    AuthorizationHandle authorizationHandle;

    /**
     * 获取系统设置
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "系统设置列表", notes = "获取当前系统设置信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Setting.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("getSetting/{pageNum}/{pageSize}")
    public JsonResult getSetting(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
//        //先判断权限
//        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
//        if (!authon.getCode().equals("200")) {
//            return authon;
//        }
        Page<Setting> settingList = adminService.getSettingList(pageNum, pageSize);
        return new JsonResult(ResultCode.SUCCESS, settingList);
    }


    /**
     * 商品價格列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "商品價格列表", notes = "获取商品價格列表信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Setting.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("getGoodsSetting/{pageNum}/{pageSize}")
    public JsonResult getGoodsSetting(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        Page<GoodsSetting> goodsSettingList = adminService.getGoodsSettingList(pageNum, pageSize);
        return new JsonResult(ResultCode.SUCCESS, goodsSettingList);
    }

    /**
     * 更新设置  只能更新值
     *
     * @param string
     * @return
     */
    @ApiOperation(value = "更新系统设置", notes = "更新当前系统设置信息，只能更新值，key不能更新")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("update/{id}")
    public JsonResult updateValue(@ApiParam(name = "id", value = "系统设置的id", required = true) @PathVariable("id") int id, @ApiParam(name = "value", value = "更新的值", required = true) @RequestBody String string, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        String value = jsonObject.get("value").getAsString();
        adminService.updateValue(id, value);
        return new JsonResult(ResultCode.SUCCESS);
    }


    /**
     * 更新商品价格
     *
     * @param string
     * @return
     */
    @ApiOperation(value = "更新商品价格", notes = "更新商品价格")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("updateGoodsPrice/{id}")
    public JsonResult updateGoodsPrice(@ApiParam(name = "id", value = "系统设置的id", required = true) @PathVariable("id") int id, @ApiParam(name = "price", value = "更新的值", required = true) @RequestBody String string, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        int price = jsonObject.get("price").getAsInt();
        adminService.updateGoodsPrice(id, price);
        return new JsonResult(ResultCode.SUCCESS);
    }

    /**
     * 管理员创建业务员
     *
     * @param admin
     * @param errors
     * @param request
     * @return
     */
    @ApiOperation(value = "创建业务员", notes = "管理员创建业务员")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口"),
            @ApiResponse(code = 1200, message = "请求字段为空")
    })
    @PostMapping("createUser")
    public JsonResult createUser(@ApiParam(name = "后台人员对象", value = "json格式", required = true) @Valid @RequestBody Admin admin, Errors errors, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ADMIN);
        if (!authon.getCode().equals("200")) {
            return authon;
        }

        if (errors.hasErrors()) {
            JsonResult jsonResult = UtilTools.handleNullField(errors);
            return jsonResult;
        }

        adminService.createUser(admin);
        return new JsonResult(ResultCode.SUCCESS);
    }

    /**
     * 删除指定业务员
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "删除业务员", notes = "管理员删除指定业务员，链接上传删除的id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("deleteUser/{id}")
    public JsonResult deleteUser(@ApiParam(name = "id", value = "删除人员的id", required = true) @PathVariable("id") int id, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ADMIN);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        adminService.deleteUser(id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    /**
     * 获取业务员列表
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "业务员列表", notes = "管理员获取业务员列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Admin.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("userList")
    public JsonResult getUserList(HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ADMIN);
        if (!authon.getCode().equals("200")) {
//            return new Gson().toJson(authon);
            return authon;
        }
        List<Admin> userList = adminService.getUserList();
        return new JsonResult(ResultCode.SUCCESS, userList);
//        return new Gson().toJson(new JsonResult(ResultCode.SUCCESS,userList));
    }

    /**
     * 后台人员更新密码
     *
     * @param string
     * @param request
     * @return
     */
    @ApiOperation(value = "后台人员更新密码", notes = "更新密码")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("updatePassword")
    public JsonResult updatePassword(@ApiParam(name = "password", value = "密码", required = true) @RequestBody String string, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        int userId = (int) authon.getData();
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        String password = jsonObject.get("password").getAsString();
        adminService.updatePassword(userId, password);
        return new JsonResult(ResultCode.SUCCESS);
    }
}
